﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization.Json;
using Hisuntech.SystemFramework.Net.Json;
using WcfMail.Interface;

namespace WcfMail.Common
{
    public class WcfJsonConverter : IConvert 
    {

        public  T ConvertToObject<T>(string objectString)
        {
            return JSONClient.GetObjectFromJsonString<T>(objectString);
        }

        public object ConvertToObject(string objectString, Type targetType)
        {
            return JSONClient.GetObjectFromJsonString(objectString, targetType);
        }

        /// <summary>
        /// 将JSON字符串转换成具有接口 T 的目标类型 targetType，不能转换targetType数组
        /// </summary>
        /// <param name="objectString">JSON字符串</param>
        /// <param name="targetType">具有接口 T 的目标类型</param>
        /// <returns></returns>
        public T ConvertToBaseObject<T>(string objectString, Type targetType) where T : class
        {
            //自定义JSON反序列化
            //var bytes = System.Text.Encoding.UTF8.GetBytes(objectString);
            //System.Xml.XmlDictionaryReaderQuotas ReaderQuotas = new System.Xml.XmlDictionaryReaderQuotas();
            //System.Xml.XmlDictionaryReader reader = JsonReaderWriterFactory.CreateJsonReader(bytes, ReaderQuotas);
            //while (reader.Read())
            //{
            //    object Value = reader.ReadContentAsObject();
            //}

            return (T)JSONClient.GetObjectFromJsonString(objectString, targetType);

        }

        /// <summary>
        /// 将JSON字符串转换成具有接口 T 的目标类型 targetType 列表
        /// </summary>
        /// <param name="objectString"></param>
        /// <param name="targetTypeList">目标类型的列表类型</param>
        /// <returns></returns>
        public  List<T> ConvertToBaseObjectList<T>(string objectString, Type targetTypeList) where T : class
        {
            List<T> list = new List<T>();
            object result = JSONClient.GetObjectFromJsonString(objectString, targetTypeList);
            if (result != null)
            {
                foreach (T item in result as IEnumerable<T>)
                {
                    list.Add(item);
                }
            }
            return list;
        }

        /// <summary>
        /// 将目标对象转换成JSON字符串
        /// </summary>
        /// <param name="targetObject">要转换的目标对象</param>
        /// <returns></returns>
        public  string ConvertToString(object  targetObject)
        {
            return JSONClient.GetJSONString(targetObject, targetObject.GetType ());
        }

        /// <summary>
        /// 将目标对象转换成指定类型的JSON字符串
        /// </summary>
        /// <param name="targetObject">要转换的目标对象</param>
        /// <param name="extType">指定要转换的类型，通常为基础类型的派生类型</param>
        /// <returns></returns>
        public  string ConvertToString(object  targetObject, Type extType)
        {
            return JSONClient.GetJSONString(targetObject, extType);
        }


       
    }
}
